<template>
  <!-- 新增编辑弹框 -->
  <a-modal
    :width="700"
    :maskClosable="false"
    :visible="props.visible"
    :confirm-loading="loading"
    :forceRender="true"
    :title="isUpdate ? '编辑煤场管理代码' : '新建煤场管理代码'"
    :body-style="{ paddingBottom: '8px', height: '500px', overflowY: 'auto' }"
    @update:visible="updateVisible"
    @ok="save"
    @close="updateVisible(false)"
  >
    <wm-md-warehouse-manage-form v-model:form="form" :siteCodeList="siteCodeList" :zoneCodeList="zoneCodeList"  ref="wmMdWarehouseManageFormRef" />
  </a-modal>
</template>

<script setup name="WmMdWarehouseManageAddEdit">
import { ref, onMounted } from 'vue';
import WmMdWarehouseManageForm from './wm-md-warehouse-manage-form.vue';
import { message } from 'ant-design-vue';
import { WmMdWarehouseManageApi } from '../api/WmMdWarehouseManageApi';
import { WmMdWarehouseApi } from '../../wm-md-warehouse/api/WmMdWarehouseApi';
import { WmMdZoneApi } from '../../wm-md-zone/api/WmMdZoneApi';


const props = defineProps({
  visible: Boolean,
  data: Object
});

const emits = defineEmits(['update:visible', 'done']);

// 弹框加载
const loading = ref(false);

// 是否是编辑状态
const isUpdate = ref(false);

// 表单数据
const form = ref({
  wmMdWarehouseManageSort: 1000,
});

// 煤场列表
const siteCodeList = ref([]);
const zoneCodeList = ref([]);

// ref
const wmMdWarehouseManageFormRef = ref(null);

onMounted(() => {
  if (props.data) {
    isUpdate.value = true;
    getDetail();
    console.log("编辑");
  } else {
    isUpdate.value = false;
    console.log("新建");
  }
  
  getSiteCodes();
  getZoneCodes();
});

// 获取详情
const getDetail = () => {
  WmMdWarehouseManageApi.detail({ managerId: props.data.managerId }).then(res => {
    form.value = Object.assign({}, res);
  });
};



// 更改弹框状态
const updateVisible = value => {
  emits('update:visible', value);
};

// 点击保存
const save = async () => {
  wmMdWarehouseManageFormRef.value.$refs.formRef.validate().then(async valid => {
    if (valid) {
      // 修改加载框为正在加载
      loading.value = true;

      let result = null;

      // 执行编辑或修改
      if (isUpdate.value) {
        result = WmMdWarehouseManageApi.edit(form.value);
      } else {
        result = WmMdWarehouseManageApi.add(form.value);
      }
      result
        .then(async result => {
          // 移除加载框
          loading.value = false;

          // 提示添加成功
          message.success(result.message);

          // 关闭弹框，通过控制visible的值，传递给父组件
          updateVisible(false);

          // 触发父组件done事件
          emits('done');
        })
        .catch(() => {
          loading.value = false;
        });
    }
  });
};

/**
 *获取所有的煤场
 *
 * @author fengshuonan
 * @date 2021/12/29 10:08:31
 */
 const getSiteCodes = async () => {
  let templates = await WmMdWarehouseApi.findList();
  siteCodeList.value = [];
  for (let template of templates) {
    if (template.delFlag === 'N') {
      siteCodeList.value.push({
        value: template.siteCode,
        label: template.siteName
      });
    }
  }
 
 
 
};

 /**
 *获取所有的分区
 *
 * @author fengshuonan
 * @date 2021/12/29 10:08:31
 */
 const getZoneCodes = async () => {
  let templates = await WmMdZoneApi.findList();
  zoneCodeList.value = [];
  for (let template of templates) {
    if (template.delFlag === 'N') {
      zoneCodeList.value.push({
        value: template.zoneCode,
        label: template.zoneName
      });
    }
  }
};
</script>

<style></style>
